import { createApp } from 'vue'
import router from './router'
import Antd from 'ant-design-vue'
import "ant-design-vue/dist/reset.css";
import App from './App.vue'
import { createPinia } from 'pinia';
// 防止用户刷新界面导致需要重新登录
import { $setToken } from './api/httpRequest'
let token = sessionStorage.getItem("token");
if (token) {
    $setToken(token);
}

// 防止用户刷新界面导致菜单信息丢失
let authInfo = JSON.parse(sessionStorage.getItem("authInfo"))
if (authInfo && authInfo.menuList) {
    addDynamicRoute(authInfo.menuList)
}
let head_menu = JSON.parse(sessionStorage.getItem("head_menu"))
if (head_menu) {
    addDynamicRoute(head_menu)
}

function addDynamicRoute(data) {
    data.forEach(item => {
      if(item.path && item.name && item.componentName){
        router.addRoute('admin', {
          path: item.path,
          name: item.name,
          meta: {
            title: item.title
          },
          component: () => import('./components/' + item.componentName + '.vue')
        })
      }
      if (item.children) {
        addDynamicRoute(item.children)
      }
    });
  }

const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.use(router)
app.use(Antd)
app.mount('#app')

